#include "TraceCallbackImpl.h"


TraceCallbackImpl::TraceCallbackImpl()
{
	m_mid_Print = 0;

}

TraceCallbackImpl::~TraceCallbackImpl(void)
{
}

bool TraceCallbackImpl::init_jni(JNIEnv* env)
{
	bool result = false;

	do 
	{
		m_mid_Print = (env)->GetMethodID(m_cls, "print", "(Lcom/voe/common/TraceLevel;Ljava/lang/String;I)V");
		if(!m_mid_Print)
			break;

		m_cls_TraceLevel = (env)->FindClass("com/voe/common/TraceLevel");
		if(!m_cls_TraceLevel)
			break;

		m_mid_TraceLevel_fromValue = (env)->GetStaticMethodID(m_cls_TraceLevel, "fromValue", "(I)Lcom/voe/common/TraceLevel;");
		if(!m_mid_TraceLevel_fromValue)
			break;

		result = true;

	} while (false);

	return result;
}

void TraceCallbackImpl::Print(webrtc::TraceLevel level, const char* message, int length)
{
	JNIEnv* env = 0;

	if(before(&env))
	{
		jobject trace_level = (env)->CallStaticObjectMethod(m_cls_TraceLevel, m_mid_TraceLevel_fromValue, (int)level);
		if(trace_level)
		{
			(env)->CallObjectMethod(m_obj, m_mid_Print, trace_level, 
				JniHelper::c2j_string(env, message), length);
		}

		after(env);
	}
}
